/*
 * Copyright (C) 2008-2009 Google Inc. 
 * Copyright (C) 2013 Winson Wong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Source is base on Android API Source 16  
 */

package myidea.winson.android.cangjieime;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.inputmethod.InputMethodSubtype;
import myidea.winson.android.cangjieime.Keyboard.Key;

public class CangjieKeyboardView extends KeyboardView {

    static final int KEYCODE_OPTIONS = -100;
    
    private int mKeySubTextSize;
    private int mKeySubTextColor;
    
    public CangjieKeyboardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CangjieKeyboardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cangjie_Keyboard_View, defStyle, 0);
        
        int n = R.styleable.KeyboardView.length;
        
        for (int i = 0; i < n; i++) {
        	int attr = i;
            switch (attr) {
            case R.styleable.Cangjie_Keyboard_View_keySubTextSize:
            	mKeySubTextSize = a.getDimensionPixelSize(attr,18);
                break;
            case R.styleable.Cangjie_Keyboard_View_keySubTextColor:
            	mKeySubTextColor = a.getColor(attr, 0xFFFFFF);
            	break;
            }
        }
    }
    
    @Override
    protected void resetKeyboardAdj(Keyboard oldKeyboard, Keyboard newKeyboard){
    	super.resetKeyboardAdj(oldKeyboard, newKeyboard);
    	if (oldKeyboard != null){
            //reset the keyboard key height
    		mKeySubTextSize -= ((CangjieKeyboard)oldKeyboard).keySubTextSizeAdj;
    	}
    	if (newKeyboard != null){
            //calculate the new keyboard key height
    		mKeySubTextSize += ((CangjieKeyboard)newKeyboard).keySubTextSizeAdj;
    	}
    }
    
    @Override
    protected boolean onLongPress(Key key) {
        if (key.codes[0] == Keyboard.KEYCODE_CANCEL) {
            getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null);
            return true;
        } else {
            return super.onLongPress(key);
        }
    }
    

    public boolean getCapsLock(){
    	return ((CangjieKeyboard)this.getKeyboard()).getCapsLock();
    }
    
    public void setCapsLock(boolean capsLock){
    	((CangjieKeyboard)this.getKeyboard()).setCapsLock(capsLock);
    }
    
    
    private CharSequence adjustCase(CharSequence subLabel, Keyboard keyboard){
    	if (keyboard.isShifted() && subLabel != null && Character.isLowerCase(subLabel.charAt(0))) {
            subLabel = subLabel.toString().toUpperCase();
        }
        return subLabel;
    }
    
    @Override
    protected void keyAdditionalDraw(Key key, final Canvas canvas, final Paint paint,
			Rect padding, Keyboard keyboard) {
    	CangjieKey _key = (CangjieKey) key;
    	
    	int orgTextColor = paint.getColor();
    	if (mKeySubTextColor != orgTextColor){
    		paint.setColor(mKeySubTextColor);
    	}
    	
    	/* Draw the sub text label to key */
    	paint.setTextSize(mKeySubTextSize);    	
    	paint.setColor(mKeySubTextColor);
    	
    	if (_key.subLabel != null){
        	String subLabelStr = _key.subLabel == null? null : adjustCase(_key.subLabel, keyboard).toString();
        	canvas.drawText(subLabelStr,
                    	padding.left,
                    	(paint.getTextSize() - paint.descent()) / 2 + padding.top,
                    paint);
    	}
    	if (mKeySubTextColor != orgTextColor){
    		paint.setColor(orgTextColor);
    	}
    	return;
    }

    /*
    void setSubtypeOnSpaceKey(final InputMethodSubtype subtype) {
        final CangjieKeyboard keyboard = (CangjieKeyboard)getKeyboard();
        keyboard.setSpaceIcon(getResources().getDrawable(subtype.getIconResId()));
        invalidateAllKeys();
    }
    */
}
